package com.atguigu.gulimall.product.service.impl;

import com.atguigu.gulimall.product.dao.AttrAttrgroupRelationDao;
import com.atguigu.gulimall.product.dao.AttrDao;
import com.atguigu.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.atguigu.gulimall.product.entity.AttrEntity;
import com.atguigu.gulimall.product.service.AttrService;
import com.atguigu.gulimall.product.vo.CategoryAttrGroupsAttrsVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.product.dao.AttrGroupDao;
import com.atguigu.gulimall.product.entity.AttrGroupEntity;
import com.atguigu.gulimall.product.service.AttrGroupService;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {


    @Resource
    AttrAttrgroupRelationDao attrgroupRelationDao;
    @Resource
    AttrDao attrDao;

    @Resource
    AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }


    //根据分类编号去查询该分类具有哪些属性
    @Override
    public PageUtils queryPage(Map<String, Object> params, Integer catelogId) {
        String key = (String) params.get("key");
        //组装业务参数
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<>();
        //当传递的有key（检索字段的时候，拼接上该字段）
        if (!StringUtils.isEmpty(key)) {
            wrapper.and((obj) -> {
                obj.eq("attr_group_id", key).or().like("attr_group_name", key);
            });
        }

        //查询默认分类编号
        if (catelogId == 0) {
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params), wrapper);
            return new PageUtils(page);

        } else {
            wrapper.eq("catelog_id", catelogId);
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params), wrapper);
            return new PageUtils(page);
        }


    }


    @Override
    public List<AttrEntity> queryRelation(Long attrgroupId) {
        List<AttrEntity> entityList = null;
        //通过属性的分组编号在中间表中查找到属性编号，在属性表中查到属性名和可选值
        QueryWrapper<AttrAttrgroupRelationEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("attr_group_id", attrgroupId);
        List<AttrAttrgroupRelationEntity> entities = attrgroupRelationDao.selectList(wrapper);
        //属性编号集合
        List<Long> ids = entities.stream().map((item) -> {
            return item.getAttrId();
        }).collect(Collectors.toList());
        //批量查找
        if (ids != null && ids.size() > 0) {
            entityList = attrDao.selectBatchIds(ids);

        }
        return entityList;
    }


    @Override
    public void delBatch(List<AttrAttrgroupRelationEntity> attrgroupRelationEntities) {

        attrgroupRelationDao.deleteBatchRelation(attrgroupRelationEntities);

    }


    @Override
    public PageUtils getNoRelation(Map<String, Object> params, Long attrgroupId) {

        PageUtils noRelation = attrService.getNoRelation(params, attrgroupId);
        return noRelation;

    }


    @Override
    public List<CategoryAttrGroupsAttrsVo> selectGroupsAttrsByCatLogId(Long cateLogId) {
//        attrService.getNoRelation(

        //获取当前分类的所有的分组
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("catelog_id", cateLogId);
        List<AttrGroupEntity> groupEntities = baseMapper.selectList(wrapper);
        List<CategoryAttrGroupsAttrsVo> attrsVos = groupEntities.stream().map(item -> {
            CategoryAttrGroupsAttrsVo vo = new CategoryAttrGroupsAttrsVo();
            //先进行一次拷贝属性
            BeanUtils.copyProperties(item, vo);

            //查询属性的分组ID
            Long attrGroupId = item.getAttrGroupId();
            //查询当前分组绑定了哪些属性ID
            List<AttrAttrgroupRelationEntity> relationEntities = attrgroupRelationDao.selectList(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_group_id", attrGroupId));
            //属性集合
            List<AttrEntity> attrs = relationEntities.stream().map(item2 -> {
                Long attrId = item2.getAttrId();
                //查询属性的详情信息
                AttrEntity attrEntity = attrService.getById(attrId);
                return attrEntity;
            }).collect(Collectors.toList());
            vo.setAttrs(attrs);
            return vo;
        }).collect(Collectors.toList());

        return attrsVos;
    }


}